<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ page import="com.ce.sys.login.model.User" %>
<%@ page import="com.ce.util.RegeditUtil" %>
<%@ taglib uri="/struts-tags" prefix="s" %>
<% 
String path = request.getContextPath(); 
String receiveUserId = request.getParameter("receiveUserId");
String receiveUserName = request.getParameter("receiveUserName");
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <script type="text/javascript">
        $(function(){
            $('#sendMsg').linkbutton();
            $('#viewMsg').linkbutton();
            $('#msg').validatebox({
                   validType:'specialHtmlChar'
               });
            $('#clearDialog').click(function(){
                $('#dialogHistory').empty();
            });
		    $('#sendMsg').click(function(){
		         if($('#msg').val()==""){
		              $.messager.alert('警告', '不能发送空信息！','error');
		              return false;
		         }else if($('#msg').val().length>300){
		         	  $.messager.alert('警告', '输入信息内容过长！','error');
		              return false;
		         }else if(!$('#msg').validatebox('isValid')){
		             return false;
		         }		         
	        jQuery.post("<%=path%>/pub/message/PubMessage_add.action?op=savejs",{"po.vcSendUser":"<%=User.getUserId() %>","po.vcReceiveUser":"<%=receiveUserId%>","po.vcContent":$('#msg').val()}, function(){
	             alldate=new Date();
		         var nowDataStr=alldate.getFullYear()+"-"+(alldate.getMonth()+1)+"-"+alldate.getDate()+"  "+alldate.getHours()+":"+alldate.getMinutes()+":"+alldate.getSeconds();
		         $('#dialogHistory').append('<font color="blue"><%=User.getUser(request).getVcName() %>   '+nowDataStr+'</font><br>'+$('#msg').val()+'<br>');
		         $('#msg').val("");
		         $('#dialogHistory').animate({scrollTop: $('#dialogHistory')[0].scrollHeight});
	           });
		    });
		    
		   	$('#viewMsg').click(function(){
		        if(!$('#opTabs').tabs('exists','与<s:property value="#parameters.receiveUserName"/>的聊天记录')){ //判断该tab是否已经存在，不存在则建立新tab
					    var content = '<iframe scrolling="no" frameborder="0"  src="<%=path%>/pub/message/PubMessage.action?receiveUser=<%=User.getUserId() %>&sendUser=<%=receiveUserId%>" style="width:100%;height:100%;"></iframe>'; 
					    $('#opTabs').tabs('add',{             //打开菜单对应的TAB页
							title:'与<s:property value="#parameters.receiveUserName"/>的聊天记录',
							closable:true,
							//href:node.id
							content:content
					     });
					   tabNames.push('与<s:property value="#parameters.receiveUserName"/>的聊天记录');     //入栈
					}else{ //将已经存在的tab进行选中
					   $('#opTabs').tabs('select','与<s:property value="#parameters.receiveUserName"/>的聊天记录');  
					}
		    });   
		    getMsg('<%=receiveUserId%>');  
		     
		   msgTimeId = setInterval("getMsg('<%=receiveUserId%>')",<%=RegeditUtil.getRegeditIntValue("REFRESH_TIME") %>);  //设定消息刷新时间，20秒刷新一次
        });
        
        function getMsg(receiveUser){
             $.getJSON("<%=path%>/pub/message/PubMessage_getNewMsg.action?sendUserId="+receiveUser+"&t="+new Date().getTime(), function(data){
                 console.log(data);
                  $.each(data, function(i,item){
                      console.log(i);
				      $('#dialogHistory').append('<font color="blue">'+item.sendUserName+'  '+item.sendTimeStr+'</font><br>'+item.vcContent+'<br>');
                      $('#dialogHistory').animate({scrollTop: $('#dialogHistory')[0].scrollHeight});
				  });
			});
        }
    </script>
  </head>
 
  <body style="padding-top:3px">
<table width="380" align="center" cellpadding="1" cellspacing="0"  leftmargin="0" topmargin="0" marginwidth="0" marginheight="0" >
            <tr><td height="130">
               <div id="dialogHistory" style="width:100%;height:100%;overflow-y:auto;border:1px solid #b8cfdb; line-height:18px"></div>
            </td></tr>
            <tr><td>
            <div style="height:17px; border:1px solid #b8cfdb; background-color:#eef7fd; width:100%; text-align:left; padding-top:2px" align="center"><font id="clearDialog" class="font_bar_del">清屏</font></div></td></tr>
            <tr><td height="50" align="center" valign="top"><textarea id="msg" name="msg" style="width:100%;height:100%;overflow-y:auto;"></textarea></td></tr>
            <tr><td align="right" height="30"><a id="viewMsg"  href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-look">消息记录</a><a id="sendMsg" href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-ok">发送</a></td></tr>
</table>

  </body>
</html>
